package cn.kys.modules.system.service.impl;


import cn.kys.modules.system.repository.GeoRepository;
import cn.kys.modules.system.service.GeoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wyq
 * @date 2020-09-21 09:32
 */
@Service("GeoService")
public class GeoServiceImpl implements GeoService {

    @Autowired
    GeoRepository geoRepository;
    @Override
    public Integer findCount(String lat, String lon,String table) {
        return geoRepository.findCount(lat, lon,table);
    }

    @Override
    public List findGeo(String lat, String lon,String table) {
        List<Map<String,Object>> list = geoRepository.findGeo(lat, lon,table);

        List calList = new ArrayList();
        DecimalFormat df   = new DecimalFormat("######0.00");
        for(int k=0;k<list.size();k++){
            Map tempMap = list.get(k);

            //重构返回的图形数据
            List listPolygon = new ArrayList();
            String st_astext = (String) tempMap.get("st_astext");

            if (st_astext.contains("MULTIPOLYGON")) {
                st_astext = st_astext.substring(15, st_astext.length() - 3);
            } else {
                st_astext = st_astext.substring(9, st_astext.length() - 2);
            }


            String[] polygons = st_astext.split("\\)\\),\\(\\(");

            for(int j=0;j<polygons.length;j++){
                String polygonTemp = polygons[j];
                if(polygonTemp.contains("),(")){
                    String[] polygonHole = polygonTemp.split("\\),\\(");
                    listPolygon.add(polygonHole);
                }else{
                    listPolygon.add(polygonTemp);
                }
            }
            tempMap.put("position", listPolygon);

            if("t_building".equals(table)){
                String shape_area = df.format(Double.parseDouble(tempMap.get("shape_area").toString()));
                String shape_leng = df.format(Double.parseDouble(tempMap.get("shape_leng").toString()));
                //String height = df.format(Double.parseDouble(tempMap.get("height").toString()));
                String code = tempMap.get("code").toString();
                String struct = tempMap.get("struct").toString();
                String starttime = tempMap.get("starttime").toString();
                String drawtime = tempMap.get("drawtime").toString();
                String floor = tempMap.get("floor").toString();

                tempMap.put("面积", shape_area);
                tempMap.put("长度", shape_leng);
                tempMap.put("邮编", code);
                tempMap.put("楼层数量", floor);
                tempMap.put("结构", struct);
                tempMap.put("测量时间", starttime);
                tempMap.put("绘图时间", drawtime);

                tempMap.remove("shape_area");
                tempMap.remove("shape_leng");
                //tempMap.remove("height");
                tempMap.remove("geom");
                tempMap.remove("st_astext");
                tempMap.remove("code");
                tempMap.remove("floor");
                tempMap.remove("struct");
                tempMap.remove("starttime");
                tempMap.remove("drawtime");
            }

            if("t_greenland".equals(table)){


                tempMap.put("名字", tempMap.get("name").toString());
                tempMap.remove("name");
                tempMap.remove("geom");
                tempMap.remove("st_astext");
            }

            calList.add(tempMap);

        }
        return calList;
    }

    @Override
    public List<Map<String, Object>> findGeoPolygon(String polygon, String types) {
        List list= new ArrayList();

        String[] typeArr = types.split("\\*");	// 分割成数组
        List listArr = new ArrayList();
        for(int i=0;i<typeArr.length;i++){
            listArr.add(typeArr[i]);
        }

        String table = null;

        List listBuilding = new ArrayList();
        List listGreenland = new ArrayList();
        if(listArr.contains("BUILDING")){

            table = "t_building";
            listBuilding = geoRepository.findGeoPolygon(polygon, table);

            List calList = new ArrayList();
            for(int i=0;i<listBuilding.size();i++){
                Map tempMap = (Map)listBuilding.get(i);

                List listPolygon = new ArrayList();
                String st_astext = (String) tempMap.get("st_astext");

                if (st_astext.contains("MULTIPOLYGON")) {
                    st_astext = st_astext.substring(15, st_astext.length() - 3);
                } else {
                    st_astext = st_astext.substring(9, st_astext.length() - 2);
                }


                String[] polygons = st_astext.split("\\)\\),\\(\\(");

                for(int j=0;j<polygons.length;j++){
                    String polygonTemp = polygons[j];
                    if(polygonTemp.contains("),(")){
                        String[] polygonHole = polygonTemp.split("\\),\\(");
                        listPolygon.add(polygonHole);
                    }else{
                        listPolygon.add(polygonTemp);
                    }
                }

                tempMap.put("postion", listPolygon);

                calList.add(tempMap);
            }

            Map<String,Object> map = new HashMap();
            map.put("layer", "建筑物");
            map.put("geos", calList);
            list.add(map);
        }
        if(listArr.contains("VEGA")){

            table = "t_greenland";
            listGreenland = geoRepository.findGeoPolygon(polygon, table);

            List calList = new ArrayList();
            for(int i=0;i<listGreenland.size();i++){
                Map tempMap = (Map)listGreenland.get(i);

                List listPolygon = new ArrayList();
                String st_astext = (String) tempMap.get("st_astext");

                if (st_astext.contains("MULTIPOLYGON")) {
                    st_astext = st_astext.substring(15, st_astext.length() - 3);
                } else {
                    st_astext = st_astext.substring(9, st_astext.length() - 2);
                }


                String[] polygons = st_astext.split("\\)\\),\\(\\(");

                for(int j=0;j<polygons.length;j++){
                    String polygonTemp = polygons[j];
                    if(polygonTemp.contains("),(")){
                        String[] polygonHole = polygonTemp.split("\\),\\(");
                        listPolygon.add(polygonHole);
                    }else{
                        listPolygon.add(polygonTemp);
                    }
                }

                tempMap.put("postion", listPolygon);

                calList.add(tempMap);
            }

            Map<String,Object> map = new HashMap();
            map.put("layer", "绿地");
            map.put("geos", calList);
            list.add(map);
        }

        return list;
    }
}
